package com.llh.controller.processdata;

import com.llh.bean.MiddleData;
import com.llh.bean.StockBase;
import com.llh.mapper.MiddleDataMapper;
import com.llh.mapper.StockBaseMapper;
import com.llh.util.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;

public class ComputeData {
    public static void computeDuration() {
        //先获取到所有的待处理的股票信息
       /* SqlSession sqlSesion = SqlSessionUtil.getSqlSesion();
        StockBaseMapper mapper = sqlSesion.getMapper(StockBaseMapper.class);
        //获取所有的需要处理的股票信息
        List<StockBase> stockBases = mapper.selectAll();
        List<MiddleData> middleDataList = new ArrayList<>();
        for (StockBase stockBase : stockBases) {
            MiddleData middleData = new MiddleData();
            //股票代码
            middleData.setS_code(stockBase.getsCode());
            //解禁日期
            middleData.setOpen_day(stockBase.getOpenDay());
            //解禁数量
            middleData.setOpen_num(stockBase.getOpenNum());
            if (stockBase.getsCode().endsWith(".SH")) {
                //上交所
                middleData.setExchange("SSE");
            } else if (stockBase.getsCode().endsWith(".SZ")) {
                //深交所
                middleData.setExchange("SZSE");
            }
            middleDataList.add(middleData);
        }
        System.out.println("预计结果数:"+middleDataList.size());
        deal(middleDataList,sqlSesion);*/

    }

    public static void main(String[] args) {
        computeDuration();
//        test();
    }
    public static void test(){
       /* //先获取到所有的待处理的股票信息
        SqlSession sqlSesion = SqlSessionUtil.getSqlSesion();
        StockBaseMapper mapper = sqlSesion.getMapper(StockBaseMapper.class);
        //获取所有的需要处理的股票信息
        List<StockBase> stockBases = mapper.selectAll();
        List<MiddleData> middleDataList = new ArrayList<>();
        for (StockBase stockBase : stockBases) {
            MiddleData middleData = new MiddleData();
            //股票代码
            middleData.setS_code(stockBase.getsCode());
            //解禁日期
            middleData.setOpen_day(stockBase.getOpenDay());
            //解禁数量
            middleData.setOpen_num(stockBase.getOpenNum());
            if (stockBase.getsCode().endsWith(".SH")) {
                //上交所
                middleData.setExchange("SSE");
            } else if (stockBase.getsCode().endsWith(".SZ")) {
                //深交所
                middleData.setExchange("SZSE");
            }
            middleDataList.add(middleData);*/
       /* }
        //取一条
        MiddleData middleData = middleDataList.get(0);
        ArrayList<MiddleData> list = new ArrayList<>();
        boolean add = list.add(middleData);
        System.out.println("预计结果数:"+list.size());
        List<MiddleData> selectMiddle = mapper.selectMiddle(list);
        for (int i = 0; i < selectMiddle.size(); i++) {
            System.out.println(i+":"+selectMiddle.get(i));
        }*/

    }

    public static void deal(List<MiddleData> middleDataList,SqlSession sqlSesion) {
        StockBaseMapper stockBaseMapper = sqlSesion.getMapper(StockBaseMapper.class);
        MiddleDataMapper middleDataMapper = sqlSesion.getMapper(MiddleDataMapper.class);
        int num = 0;
        int pointsDataLimit = 100;
        List<MiddleData> newList = new ArrayList<MiddleData>();
        for (int i = 0; i < middleDataList.size(); i++) {//分批次处理
            newList.add(middleDataList.get(i));

            if (pointsDataLimit == newList.size() || i == middleDataList.size() - 1) {
                //查数据
                List<MiddleData> selectMiddle = stockBaseMapper.selectMiddle(newList);
                //插入数据
                int insert = middleDataMapper.insetBatch(selectMiddle);
                System.out.println("成功插入:"+insert);
                num += newList.size();
                newList.clear();

            }


        }
        System.out.println("总共处理了:"+num);
    }
}
